class Solution {
public:
    void reverse_str(string& str, int left, int right)
    {
        while(left < right)
        {
            swap(str[left++], str[right--]);
        }
    }
    int reverse(int x) {
        string str = to_string(x);
        int sz = str.size();
        int tmp = sz - 1;
        while(tmp > 0 && str[tmp] == '0')
        {
            tmp--;
        }
        if(str[0] == '-')
            reverse_str(str, 1, tmp);
        else
            reverse_str(str, 0, tmp);
        long long ret = atoll(str.substr(0, tmp + 1).c_str());
        return (ret >= INT_MAX || ret <= INT_MIN) ? 0 : ret;
    }
};